package com.andrognito.patternlockview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import androidx.vectordrawable.graphics.drawable.AndroidResources;
import com.andrognito.patternlockview.listener.PatternLockViewListener;
import com.andrognito.patternlockview.utils.PatternLockUtils;
import com.andrognito.patternlockview.utils.ResourceUtils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes.dex */
public class PatternLockView extends View {
    private static final int DEFAULT_DOT_ANIMATION_DURATION = 190;
    private static final float DEFAULT_DRAG_THRESHOLD = 0.0f;
    private static final int DEFAULT_PATH_END_ANIMATION_DURATION = 100;
    private static final int DEFAULT_PATTERN_DOT_COUNT = 3;
    private static final int MILLIS_PER_CIRCLE_ANIMATING = 700;
    private static final boolean PROFILE_DRAWING = false;
    private static int sDotCount;
    private long mAnimatingPeriodStart;
    private int mAspectRatio;
    private boolean mAspectRatioEnabled;
    private int mCorrectStateColor;
    private final Path mCurrentPath;
    private int mDotAnimationDuration;
    private int mDotNormalSize;
    private Paint mDotPaint;
    private int mDotSelectedSize;
    private DotState[][] mDotStates;
    private boolean mDrawingProfilingStarted;
    private boolean mEnableHapticFeedback;
    private Interpolator mFastOutSlowInInterpolator;
    private float mHitFactor;
    private float mInProgressX;
    private float mInProgressY;
    private boolean mInStealthMode;
    private boolean mInputEnabled;
    private final Rect mInvalidate;
    private Interpolator mLinearOutSlowInInterpolator;
    private int mNormalStateColor;
    private int mPathEndAnimationDuration;
    private Paint mPathPaint;
    private int mPathWidth;
    private ArrayList<Dot> mPattern;
    private boolean[][] mPatternDrawLookup;
    private boolean mPatternInProgress;
    private List<PatternLockViewListener> mPatternListeners;
    private int mPatternSize;
    private int mPatternViewMode;
    private final Rect mTempInvalidateRect;
    private float mViewHeight;
    private float mViewWidth;
    private int mWrongStateColor;

    @Retention(RetentionPolicy.SOURCE)
    /* loaded from: classes.dex */
    public @interface AspectRatio {
        public static final int ASPECT_RATIO_HEIGHT_BIAS = 2;
        public static final int ASPECT_RATIO_SQUARE = 0;
        public static final int ASPECT_RATIO_WIDTH_BIAS = 1;
    }

    /* loaded from: classes.dex */
    public static class DotState {
        ValueAnimator mLineAnimator;
        float mSize;
        float mScale = 1.0f;
        float mTranslateY = 0.0f;
        float mAlpha = 1.0f;
        float mLineEndX = Float.MIN_VALUE;
        float mLineEndY = Float.MIN_VALUE;
    }

    @Retention(RetentionPolicy.SOURCE)
    /* loaded from: classes.dex */
    public @interface PatternViewMode {
        public static final int AUTO_DRAW = 1;
        public static final int CORRECT = 0;
        public static final int WRONG = 2;
    }

    public PatternLockView(Context context) {
        this(context, null);
    }

    public PatternLockView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mDrawingProfilingStarted = false;
        this.mHitFactor = 0.6f;
        this.mInProgressX = -1.0f;
        this.mInProgressY = -1.0f;
        this.mPatternViewMode = 0;
        this.mInputEnabled = true;
        this.mInStealthMode = false;
        this.mEnableHapticFeedback = true;
        this.mPatternInProgress = false;
        this.mCurrentPath = new Path();
        this.mInvalidate = new Rect();
        this.mTempInvalidateRect = new Rect();
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.PatternLockView);
        try {
            sDotCount = obtainStyledAttributes.getInt(R.styleable.PatternLockView_dotCount, 3);
            this.mAspectRatioEnabled = obtainStyledAttributes.getBoolean(R.styleable.PatternLockView_aspectRatioEnabled, false);
            this.mAspectRatio = obtainStyledAttributes.getInt(R.styleable.PatternLockView_aspectRatio, 0);
            this.mPathWidth = (int) obtainStyledAttributes.getDimension(R.styleable.PatternLockView_pathWidth, ResourceUtils.getDimensionInPx(getContext(), R.dimen.pattern_lock_path_width));
            this.mNormalStateColor = obtainStyledAttributes.getColor(R.styleable.PatternLockView_normalStateColor, ResourceUtils.getColor(getContext(), R.color.white));
            this.mCorrectStateColor = obtainStyledAttributes.getColor(R.styleable.PatternLockView_correctStateColor, ResourceUtils.getColor(getContext(), R.color.white));
            this.mWrongStateColor = obtainStyledAttributes.getColor(R.styleable.PatternLockView_wrongStateColor, ResourceUtils.getColor(getContext(), R.color.pomegranate));
            this.mDotNormalSize = (int) obtainStyledAttributes.getDimension(R.styleable.PatternLockView_dotNormalSize, ResourceUtils.getDimensionInPx(getContext(), R.dimen.pattern_lock_dot_size));
            this.mDotSelectedSize = (int) obtainStyledAttributes.getDimension(R.styleable.PatternLockView_dotSelectedSize, ResourceUtils.getDimensionInPx(getContext(), R.dimen.pattern_lock_dot_selected_size));
            this.mDotAnimationDuration = obtainStyledAttributes.getInt(R.styleable.PatternLockView_dotAnimationDuration, DEFAULT_DOT_ANIMATION_DURATION);
            this.mPathEndAnimationDuration = obtainStyledAttributes.getInt(R.styleable.PatternLockView_pathEndAnimationDuration, 100);
            obtainStyledAttributes.recycle();
            int i = sDotCount;
            this.mPatternSize = i * i;
            this.mPattern = new ArrayList<>(this.mPatternSize);
            int i2 = sDotCount;
            this.mPatternDrawLookup = (boolean[][]) Array.newInstance(boolean.class, i2, i2);
            int i3 = sDotCount;
            this.mDotStates = (DotState[][]) Array.newInstance(DotState.class, i3, i3);
            for (int i4 = 0; i4 < sDotCount; i4++) {
                for (int i5 = 0; i5 < sDotCount; i5++) {
                    this.mDotStates[i4][i5] = new DotState();
                    this.mDotStates[i4][i5].mSize = this.mDotNormalSize;
                }
            }
            this.mPatternListeners = new ArrayList();
            initView();
        } catch (Throwable th) {
            obtainStyledAttributes.recycle();
            throw th;
        }
    }

    private void initView() {
        setClickable(true);
        Paint paint = new Paint();
        this.mPathPaint = paint;
        paint.setAntiAlias(true);
        this.mPathPaint.setDither(true);
        this.mPathPaint.setColor(this.mNormalStateColor);
        this.mPathPaint.setStyle(Paint.Style.STROKE);
        this.mPathPaint.setStrokeJoin(Paint.Join.ROUND);
        this.mPathPaint.setStrokeCap(Paint.Cap.ROUND);
        this.mPathPaint.setStrokeWidth(this.mPathWidth);
        Paint paint2 = new Paint();
        this.mDotPaint = paint2;
        paint2.setAntiAlias(true);
        this.mDotPaint.setDither(true);
        if (Build.VERSION.SDK_INT < 21 || isInEditMode()) {
            return;
        }
        this.mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(getContext(), AndroidResources.FAST_OUT_SLOW_IN);
        this.mLinearOutSlowInInterpolator = AnimationUtils.loadInterpolator(getContext(), AndroidResources.LINEAR_OUT_SLOW_IN);
    }

    @Override // android.view.View
    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        if (this.mAspectRatioEnabled) {
            int resolveMeasured = resolveMeasured(i, getSuggestedMinimumWidth());
            int resolveMeasured2 = resolveMeasured(i2, getSuggestedMinimumHeight());
            int i3 = this.mAspectRatio;
            if (i3 == 0) {
                resolveMeasured = Math.min(resolveMeasured, resolveMeasured2);
                resolveMeasured2 = resolveMeasured;
            } else if (i3 == 1) {
                resolveMeasured2 = Math.min(resolveMeasured, resolveMeasured2);
            } else if (i3 == 2) {
                resolveMeasured = Math.min(resolveMeasured, resolveMeasured2);
            } else {
                throw new IllegalStateException("Unknown aspect ratio");
            }
            setMeasuredDimension(resolveMeasured, resolveMeasured2);
        }
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        ArrayList<Dot> arrayList = this.mPattern;
        int size = arrayList.size();
        boolean[][] zArr = this.mPatternDrawLookup;
        int i = 0;
        if (this.mPatternViewMode == 1) {
            int elapsedRealtime = ((int) (SystemClock.elapsedRealtime() - this.mAnimatingPeriodStart)) % ((size + 1) * 700);
            int i2 = elapsedRealtime / 700;
            clearPatternDrawLookup();
            for (int i3 = 0; i3 < i2; i3++) {
                Dot dot = arrayList.get(i3);
                zArr[dot.mRow][dot.mColumn] = true;
            }
            if (i2 > 0 && i2 < size) {
                float f = (elapsedRealtime % 700) / 700.0f;
                Dot dot2 = arrayList.get(i2 - 1);
                float centerXForColumn = getCenterXForColumn(dot2.mColumn);
                float centerYForRow = getCenterYForRow(dot2.mRow);
                Dot dot3 = arrayList.get(i2);
                this.mInProgressX = centerXForColumn + ((getCenterXForColumn(dot3.mColumn) - centerXForColumn) * f);
                this.mInProgressY = centerYForRow + (f * (getCenterYForRow(dot3.mRow) - centerYForRow));
            }
            invalidate();
        }
        Path path = this.mCurrentPath;
        path.rewind();
        for (int i4 = 0; i4 < sDotCount; i4++) {
            float centerYForRow2 = getCenterYForRow(i4);
            int i5 = 0;
            while (i5 < sDotCount) {
                DotState dotState = this.mDotStates[i4][i5];
                drawCircle(canvas, (int) getCenterXForColumn(i5), ((int) centerYForRow2) + dotState.mTranslateY, dotState.mSize * dotState.mScale, zArr[i4][i5], dotState.mAlpha);
                i5++;
                centerYForRow2 = centerYForRow2;
            }
        }
        if (!this.mInStealthMode) {
            this.mPathPaint.setColor(getCurrentColor(true));
            float f2 = 0.0f;
            float f3 = 0.0f;
            boolean z = false;
            while (i < size) {
                Dot dot4 = arrayList.get(i);
                if (!zArr[dot4.mRow][dot4.mColumn]) {
                    break;
                }
                float centerXForColumn2 = getCenterXForColumn(dot4.mColumn);
                float centerYForRow3 = getCenterYForRow(dot4.mRow);
                if (i != 0) {
                    DotState dotState2 = this.mDotStates[dot4.mRow][dot4.mColumn];
                    path.rewind();
                    path.moveTo(f2, f3);
                    if (dotState2.mLineEndX != Float.MIN_VALUE && dotState2.mLineEndY != Float.MIN_VALUE) {
                        path.lineTo(dotState2.mLineEndX, dotState2.mLineEndY);
                    } else {
                        path.lineTo(centerXForColumn2, centerYForRow3);
                    }
                    canvas.drawPath(path, this.mPathPaint);
                }
                i++;
                f2 = centerXForColumn2;
                f3 = centerYForRow3;
                z = true;
            }
            if ((this.mPatternInProgress || this.mPatternViewMode == 1) && z) {
                path.rewind();
                path.moveTo(f2, f3);
                path.lineTo(this.mInProgressX, this.mInProgressY);
                this.mPathPaint.setAlpha((int) (calculateLastSegmentAlpha(this.mInProgressX, this.mInProgressY, f2, f3) * 255.0f));
                canvas.drawPath(path, this.mPathPaint);
            }
        }
    }

    @Override // android.view.View
    protected void onSizeChanged(int i, int i2, int i3, int i4) {
        this.mViewWidth = ((i - getPaddingLeft()) - getPaddingRight()) / sDotCount;
        this.mViewHeight = ((i2 - getPaddingTop()) - getPaddingBottom()) / sDotCount;
    }

    @Override // android.view.View
    protected Parcelable onSaveInstanceState() {
        return new SavedState(super.onSaveInstanceState(), PatternLockUtils.patternToString(this, this.mPattern), this.mPatternViewMode, this.mInputEnabled, this.mInStealthMode, this.mEnableHapticFeedback);
    }

    @Override // android.view.View
    protected void onRestoreInstanceState(Parcelable parcelable) {
        SavedState savedState = (SavedState) parcelable;
        super.onRestoreInstanceState(savedState.getSuperState());
        setPattern(0, PatternLockUtils.stringToPattern(this, savedState.getSerializedPattern()));
        this.mPatternViewMode = savedState.getDisplayMode();
        this.mInputEnabled = savedState.isInputEnabled();
        this.mInStealthMode = savedState.isInStealthMode();
        this.mEnableHapticFeedback = savedState.isTactileFeedbackEnabled();
    }

    @Override // android.view.View
    public boolean onHoverEvent(MotionEvent motionEvent) {
        if (((AccessibilityManager) getContext().getSystemService("accessibility")).isTouchExplorationEnabled()) {
            int action = motionEvent.getAction();
            if (action == 7) {
                motionEvent.setAction(2);
            } else if (action == 9) {
                motionEvent.setAction(0);
            } else if (action == 10) {
                motionEvent.setAction(1);
            }
            onTouchEvent(motionEvent);
            motionEvent.setAction(action);
        }
        return super.onHoverEvent(motionEvent);
    }

    @Override // android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (this.mInputEnabled && isEnabled()) {
            int action = motionEvent.getAction();
            if (action == 0) {
                handleActionDown(motionEvent);
                return true;
            } else if (action == 1) {
                handleActionUp(motionEvent);
                return true;
            } else if (action == 2) {
                handleActionMove(motionEvent);
                return true;
            } else if (action != 3) {
                return false;
            } else {
                this.mPatternInProgress = false;
                resetPattern();
                notifyPatternCleared();
                return true;
            }
        }
        return false;
    }

    public List<Dot> getPattern() {
        return (List) this.mPattern.clone();
    }

    public int getPatternViewMode() {
        return this.mPatternViewMode;
    }

    public boolean isInStealthMode() {
        return this.mInStealthMode;
    }

    public boolean isTactileFeedbackEnabled() {
        return this.mEnableHapticFeedback;
    }

    public boolean isInputEnabled() {
        return this.mInputEnabled;
    }

    public int getDotCount() {
        return sDotCount;
    }

    public boolean isAspectRatioEnabled() {
        return this.mAspectRatioEnabled;
    }

    public int getAspectRatio() {
        return this.mAspectRatio;
    }

    public int getNormalStateColor() {
        return this.mNormalStateColor;
    }

    public int getWrongStateColor() {
        return this.mWrongStateColor;
    }

    public int getCorrectStateColor() {
        return this.mCorrectStateColor;
    }

    public int getPathWidth() {
        return this.mPathWidth;
    }

    public int getDotNormalSize() {
        return this.mDotNormalSize;
    }

    public int getDotSelectedSize() {
        return this.mDotSelectedSize;
    }

    public int getPatternSize() {
        return this.mPatternSize;
    }

    public int getDotAnimationDuration() {
        return this.mDotAnimationDuration;
    }

    public int getPathEndAnimationDuration() {
        return this.mPathEndAnimationDuration;
    }

    public void setPattern(int i, List<Dot> list) {
        this.mPattern.clear();
        this.mPattern.addAll(list);
        clearPatternDrawLookup();
        for (Dot dot : list) {
            this.mPatternDrawLookup[dot.mRow][dot.mColumn] = true;
        }
        setViewMode(i);
    }

    public void setViewMode(int i) {
        this.mPatternViewMode = i;
        if (i == 1) {
            if (this.mPattern.size() == 0) {
                throw new IllegalStateException("you must have a pattern to animate if you want to set the display mode to animate");
            }
            this.mAnimatingPeriodStart = SystemClock.elapsedRealtime();
            Dot dot = this.mPattern.get(0);
            this.mInProgressX = getCenterXForColumn(dot.mColumn);
            this.mInProgressY = getCenterYForRow(dot.mRow);
            clearPatternDrawLookup();
        }
        invalidate();
    }

    public void setDotCount(int i) {
        sDotCount = i;
        this.mPatternSize = i * i;
        this.mPattern = new ArrayList<>(this.mPatternSize);
        int i2 = sDotCount;
        this.mPatternDrawLookup = (boolean[][]) Array.newInstance(boolean.class, i2, i2);
        int i3 = sDotCount;
        this.mDotStates = (DotState[][]) Array.newInstance(DotState.class, i3, i3);
        for (int i4 = 0; i4 < sDotCount; i4++) {
            for (int i5 = 0; i5 < sDotCount; i5++) {
                this.mDotStates[i4][i5] = new DotState();
                this.mDotStates[i4][i5].mSize = this.mDotNormalSize;
            }
        }
        requestLayout();
        invalidate();
    }

    public void setAspectRatioEnabled(boolean z) {
        this.mAspectRatioEnabled = z;
        requestLayout();
    }

    public void setAspectRatio(int i) {
        this.mAspectRatio = i;
        requestLayout();
    }

    public void setNormalStateColor(int i) {
        this.mNormalStateColor = i;
    }

    public void setWrongStateColor(int i) {
        this.mWrongStateColor = i;
    }

    public void setCorrectStateColor(int i) {
        this.mCorrectStateColor = i;
    }

    public void setPathWidth(int i) {
        this.mPathWidth = i;
        initView();
        invalidate();
    }

    public void setDotNormalSize(int i) {
        this.mDotNormalSize = i;
        for (int i2 = 0; i2 < sDotCount; i2++) {
            for (int i3 = 0; i3 < sDotCount; i3++) {
                this.mDotStates[i2][i3] = new DotState();
                this.mDotStates[i2][i3].mSize = this.mDotNormalSize;
            }
        }
        invalidate();
    }

    public void setDotSelectedSize(int i) {
        this.mDotSelectedSize = i;
    }

    public void setDotAnimationDuration(int i) {
        this.mDotAnimationDuration = i;
        invalidate();
    }

    public void setPathEndAnimationDuration(int i) {
        this.mPathEndAnimationDuration = i;
    }

    public void setInStealthMode(boolean z) {
        this.mInStealthMode = z;
    }

    public void setTactileFeedbackEnabled(boolean z) {
        this.mEnableHapticFeedback = z;
    }

    public void setInputEnabled(boolean z) {
        this.mInputEnabled = z;
    }

    public void setEnableHapticFeedback(boolean z) {
        this.mEnableHapticFeedback = z;
    }

    public void addPatternLockListener(PatternLockViewListener patternLockViewListener) {
        this.mPatternListeners.add(patternLockViewListener);
    }

    public void removePatternLockListener(PatternLockViewListener patternLockViewListener) {
        this.mPatternListeners.remove(patternLockViewListener);
    }

    public void clearPattern() {
        resetPattern();
    }

    private int resolveMeasured(int i, int i2) {
        int size = View.MeasureSpec.getSize(i);
        int mode = View.MeasureSpec.getMode(i);
        if (mode != Integer.MIN_VALUE) {
            return mode != 0 ? size : i2;
        }
        return Math.max(size, i2);
    }

    private void notifyPatternProgress() {
        sendAccessEvent(R.string.message_pattern_dot_added);
        notifyListenersProgress(this.mPattern);
    }

    private void notifyPatternStarted() {
        sendAccessEvent(R.string.message_pattern_started);
        notifyListenersStarted();
    }

    private void notifyPatternDetected() {
        sendAccessEvent(R.string.message_pattern_detected);
        notifyListenersComplete(this.mPattern);
    }

    private void notifyPatternCleared() {
        sendAccessEvent(R.string.message_pattern_cleared);
        notifyListenersCleared();
    }

    private void resetPattern() {
        this.mPattern.clear();
        clearPatternDrawLookup();
        this.mPatternViewMode = 0;
        invalidate();
    }

    private void notifyListenersStarted() {
        for (PatternLockViewListener patternLockViewListener : this.mPatternListeners) {
            if (patternLockViewListener != null) {
                patternLockViewListener.onStarted();
            }
        }
    }

    private void notifyListenersProgress(List<Dot> list) {
        for (PatternLockViewListener patternLockViewListener : this.mPatternListeners) {
            if (patternLockViewListener != null) {
                patternLockViewListener.onProgress(list);
            }
        }
    }

    private void notifyListenersComplete(List<Dot> list) {
        for (PatternLockViewListener patternLockViewListener : this.mPatternListeners) {
            if (patternLockViewListener != null) {
                patternLockViewListener.onComplete(list);
            }
        }
    }

    private void notifyListenersCleared() {
        for (PatternLockViewListener patternLockViewListener : this.mPatternListeners) {
            if (patternLockViewListener != null) {
                patternLockViewListener.onCleared();
            }
        }
    }

    private void clearPatternDrawLookup() {
        for (int i = 0; i < sDotCount; i++) {
            for (int i2 = 0; i2 < sDotCount; i2++) {
                this.mPatternDrawLookup[i][i2] = false;
            }
        }
    }

    private Dot detectAndAddHit(float f, float f2) {
        Dot checkForNewHit = checkForNewHit(f, f2);
        Dot dot = null;
        if (checkForNewHit != null) {
            ArrayList<Dot> arrayList = this.mPattern;
            if (!arrayList.isEmpty()) {
                Dot dot2 = arrayList.get(arrayList.size() - 1);
                int i = checkForNewHit.mRow - dot2.mRow;
                int i2 = checkForNewHit.mColumn - dot2.mColumn;
                int i3 = dot2.mRow;
                int i4 = dot2.mColumn;
                if (Math.abs(i) == 2 && Math.abs(i2) != 1) {
                    i3 = dot2.mRow + (i > 0 ? 1 : -1);
                }
                if (Math.abs(i2) == 2 && Math.abs(i) != 1) {
                    i4 = dot2.mColumn + (i2 > 0 ? 1 : -1);
                }
                dot = Dot.of(i3, i4);
            }
            if (dot != null && !this.mPatternDrawLookup[dot.mRow][dot.mColumn]) {
                addCellToPattern(dot);
            }
            addCellToPattern(checkForNewHit);
            if (this.mEnableHapticFeedback) {
                performHapticFeedback(1, 3);
            }
            return checkForNewHit;
        }
        return null;
    }

    private void addCellToPattern(Dot dot) {
        this.mPatternDrawLookup[dot.mRow][dot.mColumn] = true;
        this.mPattern.add(dot);
        if (!this.mInStealthMode) {
            startDotSelectedAnimation(dot);
        }
        notifyPatternProgress();
    }

    private void startDotSelectedAnimation(Dot dot) {
        final DotState dotState = this.mDotStates[dot.mRow][dot.mColumn];
        startSizeAnimation(this.mDotNormalSize, this.mDotSelectedSize, this.mDotAnimationDuration, this.mLinearOutSlowInInterpolator, dotState, new Runnable() { // from class: com.andrognito.patternlockview.PatternLockView.1
            @Override // java.lang.Runnable
            public void run() {
                PatternLockView patternLockView = PatternLockView.this;
                patternLockView.startSizeAnimation(patternLockView.mDotSelectedSize, PatternLockView.this.mDotNormalSize, PatternLockView.this.mDotAnimationDuration, PatternLockView.this.mFastOutSlowInInterpolator, dotState, null);
            }
        });
        startLineEndAnimation(dotState, this.mInProgressX, this.mInProgressY, getCenterXForColumn(dot.mColumn), getCenterYForRow(dot.mRow));
    }

    private void startLineEndAnimation(final DotState dotState, final float f, final float f2, final float f3, final float f4) {
        ValueAnimator ofFloat = ValueAnimator.ofFloat(0.0f, 1.0f);
        ofFloat.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { // from class: com.andrognito.patternlockview.PatternLockView.2
            @Override // android.animation.ValueAnimator.AnimatorUpdateListener
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float floatValue = ((Float) valueAnimator.getAnimatedValue()).floatValue();
                float f5 = 1.0f - floatValue;
                dotState.mLineEndX = (f * f5) + (f3 * floatValue);
                dotState.mLineEndY = (f5 * f2) + (floatValue * f4);
                PatternLockView.this.invalidate();
            }
        });
        ofFloat.addListener(new AnimatorListenerAdapter() { // from class: com.andrognito.patternlockview.PatternLockView.3
            @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
            public void onAnimationEnd(Animator animator) {
                dotState.mLineAnimator = null;
            }
        });
        ofFloat.setInterpolator(this.mFastOutSlowInInterpolator);
        ofFloat.setDuration(this.mPathEndAnimationDuration);
        ofFloat.start();
        dotState.mLineAnimator = ofFloat;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void startSizeAnimation(float f, float f2, long j, Interpolator interpolator, final DotState dotState, final Runnable runnable) {
        ValueAnimator ofFloat = ValueAnimator.ofFloat(f, f2);
        ofFloat.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { // from class: com.andrognito.patternlockview.PatternLockView.4
            @Override // android.animation.ValueAnimator.AnimatorUpdateListener
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dotState.mSize = ((Float) valueAnimator.getAnimatedValue()).floatValue();
                PatternLockView.this.invalidate();
            }
        });
        if (runnable != null) {
            ofFloat.addListener(new AnimatorListenerAdapter() { // from class: com.andrognito.patternlockview.PatternLockView.5
                @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
                public void onAnimationEnd(Animator animator) {
                    Runnable runnable2 = runnable;
                    if (runnable2 != null) {
                        runnable2.run();
                    }
                }
            });
        }
        ofFloat.setInterpolator(interpolator);
        ofFloat.setDuration(j);
        ofFloat.start();
    }

    private Dot checkForNewHit(float f, float f2) {
        int columnHit;
        int rowHit = getRowHit(f2);
        if (rowHit >= 0 && (columnHit = getColumnHit(f)) >= 0 && !this.mPatternDrawLookup[rowHit][columnHit]) {
            return Dot.of(rowHit, columnHit);
        }
        return null;
    }

    private int getRowHit(float f) {
        float f2 = this.mViewHeight;
        float f3 = this.mHitFactor * f2;
        float paddingTop = getPaddingTop() + ((f2 - f3) / 2.0f);
        for (int i = 0; i < sDotCount; i++) {
            float f4 = (i * f2) + paddingTop;
            if (f >= f4 && f <= f4 + f3) {
                return i;
            }
        }
        return -1;
    }

    private int getColumnHit(float f) {
        float f2 = this.mViewWidth;
        float f3 = this.mHitFactor * f2;
        float paddingLeft = getPaddingLeft() + ((f2 - f3) / 2.0f);
        for (int i = 0; i < sDotCount; i++) {
            float f4 = (i * f2) + paddingLeft;
            if (f >= f4 && f <= f4 + f3) {
                return i;
            }
        }
        return -1;
    }

    private void handleActionMove(MotionEvent motionEvent) {
        float x;
        float y;
        float f = this.mPathWidth;
        int historySize = motionEvent.getHistorySize();
        this.mTempInvalidateRect.setEmpty();
        boolean z = false;
        for (int i = 0; i < historySize + 1; i++) {
            if (i < historySize) {
                x = motionEvent.getHistoricalX(i);
            } else {
                x = motionEvent.getX();
            }
            if (i < historySize) {
                y = motionEvent.getHistoricalY(i);
            } else {
                y = motionEvent.getY();
            }
            Dot detectAndAddHit = detectAndAddHit(x, y);
            int size = this.mPattern.size();
            if (detectAndAddHit != null && size == 1) {
                this.mPatternInProgress = true;
                notifyPatternStarted();
            }
            z = (Math.abs(x - this.mInProgressX) > 0.0f || Math.abs(y - this.mInProgressY) > 0.0f) ? true : true;
            if (this.mPatternInProgress && size > 0) {
                Dot dot = this.mPattern.get(size - 1);
                float centerXForColumn = getCenterXForColumn(dot.mColumn);
                float centerYForRow = getCenterYForRow(dot.mRow);
                float min = Math.min(centerXForColumn, x) - f;
                float max = Math.max(centerXForColumn, x) + f;
                float min2 = Math.min(centerYForRow, y) - f;
                float max2 = Math.max(centerYForRow, y) + f;
                if (detectAndAddHit != null) {
                    float f2 = this.mViewWidth * 0.5f;
                    float f3 = this.mViewHeight * 0.5f;
                    float centerXForColumn2 = getCenterXForColumn(detectAndAddHit.mColumn);
                    float centerYForRow2 = getCenterYForRow(detectAndAddHit.mRow);
                    min = Math.min(centerXForColumn2 - f2, min);
                    max = Math.max(centerXForColumn2 + f2, max);
                    min2 = Math.min(centerYForRow2 - f3, min2);
                    max2 = Math.max(centerYForRow2 + f3, max2);
                }
                this.mTempInvalidateRect.union(Math.round(min), Math.round(min2), Math.round(max), Math.round(max2));
            }
        }
        this.mInProgressX = motionEvent.getX();
        this.mInProgressY = motionEvent.getY();
        if (z) {
            this.mInvalidate.union(this.mTempInvalidateRect);
            invalidate(this.mInvalidate);
            this.mInvalidate.set(this.mTempInvalidateRect);
        }
    }

    private void sendAccessEvent(int i) {
        if (Build.VERSION.SDK_INT < 16) {
            setContentDescription(getContext().getString(i));
            sendAccessibilityEvent(4);
            setContentDescription(null);
            return;
        }
        announceForAccessibility(getContext().getString(i));
    }

    private void handleActionUp(MotionEvent motionEvent) {
        if (this.mPattern.isEmpty()) {
            return;
        }
        this.mPatternInProgress = false;
        cancelLineAnimations();
        notifyPatternDetected();
        invalidate();
    }

    private void cancelLineAnimations() {
        for (int i = 0; i < sDotCount; i++) {
            for (int i2 = 0; i2 < sDotCount; i2++) {
                DotState dotState = this.mDotStates[i][i2];
                if (dotState.mLineAnimator != null) {
                    dotState.mLineAnimator.cancel();
                    dotState.mLineEndX = Float.MIN_VALUE;
                    dotState.mLineEndY = Float.MIN_VALUE;
                }
            }
        }
    }

    private void handleActionDown(MotionEvent motionEvent) {
        resetPattern();
        float x = motionEvent.getX();
        float y = motionEvent.getY();
        Dot detectAndAddHit = detectAndAddHit(x, y);
        if (detectAndAddHit != null) {
            this.mPatternInProgress = true;
            this.mPatternViewMode = 0;
            notifyPatternStarted();
        } else {
            this.mPatternInProgress = false;
            notifyPatternCleared();
        }
        if (detectAndAddHit != null) {
            float centerXForColumn = getCenterXForColumn(detectAndAddHit.mColumn);
            float centerYForRow = getCenterYForRow(detectAndAddHit.mRow);
            float f = this.mViewWidth / 2.0f;
            float f2 = this.mViewHeight / 2.0f;
            invalidate((int) (centerXForColumn - f), (int) (centerYForRow - f2), (int) (centerXForColumn + f), (int) (centerYForRow + f2));
        }
        this.mInProgressX = x;
        this.mInProgressY = y;
    }

    private float getCenterXForColumn(int i) {
        float f = this.mViewWidth;
        return getPaddingLeft() + (i * f) + (f / 2.0f);
    }

    private float getCenterYForRow(int i) {
        float f = this.mViewHeight;
        return getPaddingTop() + (i * f) + (f / 2.0f);
    }

    private float calculateLastSegmentAlpha(float f, float f2, float f3, float f4) {
        float f5 = f - f3;
        float f6 = f2 - f4;
        return Math.min(1.0f, Math.max(0.0f, ((((float) Math.sqrt((f5 * f5) + (f6 * f6))) / this.mViewWidth) - 0.3f) * 4.0f));
    }

    private int getCurrentColor(boolean z) {
        if (!z || this.mInStealthMode || this.mPatternInProgress) {
            return this.mNormalStateColor;
        }
        int i = this.mPatternViewMode;
        if (i == 2) {
            return this.mWrongStateColor;
        }
        if (i == 0 || i == 1) {
            return this.mCorrectStateColor;
        }
        throw new IllegalStateException("Unknown view mode " + this.mPatternViewMode);
    }

    private void drawCircle(Canvas canvas, float f, float f2, float f3, boolean z, float f4) {
        this.mDotPaint.setColor(getCurrentColor(z));
        this.mDotPaint.setAlpha((int) (f4 * 255.0f));
        canvas.drawCircle(f, f2, f3 / 2.0f, this.mDotPaint);
    }

    /* loaded from: classes.dex */
    public static class Dot implements Parcelable {
        public static final Parcelable.Creator<Dot> CREATOR;
        private static Dot[][] sDots = (Dot[][]) Array.newInstance(Dot.class, PatternLockView.sDotCount, PatternLockView.sDotCount);
        private int mColumn;
        private int mRow;

        @Override // android.os.Parcelable
        public int describeContents() {
            return 0;
        }

        static {
            for (int i = 0; i < PatternLockView.sDotCount; i++) {
                for (int i2 = 0; i2 < PatternLockView.sDotCount; i2++) {
                    sDots[i][i2] = new Dot(i, i2);
                }
            }
            CREATOR = new Parcelable.Creator<Dot>() { // from class: com.andrognito.patternlockview.PatternLockView.Dot.1
                /* JADX WARN: Can't rename method to resolve collision */
                @Override // android.os.Parcelable.Creator
                public Dot createFromParcel(Parcel parcel) {
                    return new Dot(parcel);
                }

                /* JADX WARN: Can't rename method to resolve collision */
                @Override // android.os.Parcelable.Creator
                public Dot[] newArray(int i3) {
                    return new Dot[i3];
                }
            };
        }

        private Dot(int i, int i2) {
            checkRange(i, i2);
            this.mRow = i;
            this.mColumn = i2;
        }

        public int getId() {
            return (this.mRow * PatternLockView.sDotCount) + this.mColumn;
        }

        public int getRow() {
            return this.mRow;
        }

        public int getColumn() {
            return this.mColumn;
        }

        public static synchronized Dot of(int i, int i2) {
            Dot dot;
            synchronized (Dot.class) {
                checkRange(i, i2);
                dot = sDots[i][i2];
            }
            return dot;
        }

        public static synchronized Dot of(int i) {
            Dot of;
            synchronized (Dot.class) {
                of = of(i / PatternLockView.sDotCount, i % PatternLockView.sDotCount);
            }
            return of;
        }

        private static void checkRange(int i, int i2) {
            if (i < 0 || i > PatternLockView.sDotCount - 1) {
                StringBuilder sb = new StringBuilder();
                sb.append("mRow must be in range 0-");
                sb.append(PatternLockView.sDotCount - 1);
                throw new IllegalArgumentException(sb.toString());
            } else if (i2 < 0 || i2 > PatternLockView.sDotCount - 1) {
                StringBuilder sb2 = new StringBuilder();
                sb2.append("mColumn must be in range 0-");
                sb2.append(PatternLockView.sDotCount - 1);
                throw new IllegalArgumentException(sb2.toString());
            }
        }

        public String toString() {
            return "(Row = " + this.mRow + ", Col = " + this.mColumn + ")";
        }

        public boolean equals(Object obj) {
            if (obj instanceof Dot) {
                Dot dot = (Dot) obj;
                return this.mColumn == dot.mColumn && this.mRow == dot.mRow;
            }
            return super.equals(obj);
        }

        public int hashCode() {
            return (this.mRow * 31) + this.mColumn;
        }

        @Override // android.os.Parcelable
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeInt(this.mColumn);
            parcel.writeInt(this.mRow);
        }

        private Dot(Parcel parcel) {
            this.mColumn = parcel.readInt();
            this.mRow = parcel.readInt();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class SavedState extends View.BaseSavedState {
        public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { // from class: com.andrognito.patternlockview.PatternLockView.SavedState.1
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // android.os.Parcelable.Creator
            public SavedState createFromParcel(Parcel parcel) {
                return new SavedState(parcel);
            }

            /* JADX WARN: Can't rename method to resolve collision */
            @Override // android.os.Parcelable.Creator
            public SavedState[] newArray(int i) {
                return new SavedState[i];
            }
        };
        private final int mDisplayMode;
        private final boolean mInStealthMode;
        private final boolean mInputEnabled;
        private final String mSerializedPattern;
        private final boolean mTactileFeedbackEnabled;

        private SavedState(Parcelable parcelable, String str, int i, boolean z, boolean z2, boolean z3) {
            super(parcelable);
            this.mSerializedPattern = str;
            this.mDisplayMode = i;
            this.mInputEnabled = z;
            this.mInStealthMode = z2;
            this.mTactileFeedbackEnabled = z3;
        }

        private SavedState(Parcel parcel) {
            super(parcel);
            this.mSerializedPattern = parcel.readString();
            this.mDisplayMode = parcel.readInt();
            this.mInputEnabled = ((Boolean) parcel.readValue(null)).booleanValue();
            this.mInStealthMode = ((Boolean) parcel.readValue(null)).booleanValue();
            this.mTactileFeedbackEnabled = ((Boolean) parcel.readValue(null)).booleanValue();
        }

        public String getSerializedPattern() {
            return this.mSerializedPattern;
        }

        public int getDisplayMode() {
            return this.mDisplayMode;
        }

        public boolean isInputEnabled() {
            return this.mInputEnabled;
        }

        public boolean isInStealthMode() {
            return this.mInStealthMode;
        }

        public boolean isTactileFeedbackEnabled() {
            return this.mTactileFeedbackEnabled;
        }

        @Override // android.view.View.BaseSavedState, android.view.AbsSavedState, android.os.Parcelable
        public void writeToParcel(Parcel parcel, int i) {
            super.writeToParcel(parcel, i);
            parcel.writeString(this.mSerializedPattern);
            parcel.writeInt(this.mDisplayMode);
            parcel.writeValue(Boolean.valueOf(this.mInputEnabled));
            parcel.writeValue(Boolean.valueOf(this.mInStealthMode));
            parcel.writeValue(Boolean.valueOf(this.mTactileFeedbackEnabled));
        }
    }
}
